package org.wizard.andy;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TableRow.LayoutParams;
import android.widget.FrameLayout;
import android.widget.TableLayout;
import android.widget.TableRow;

public class NuovaProva extends Activity implements OnClickListener { 
	
	private Integer[] facesID = {R.drawable.chi_alex, R.drawable.chi_alfred, 
			R.drawable.chi_anita, R.drawable.chi_anne, R.drawable.chi_bernard, 
			R.drawable.chi_bill, R.drawable.chi_charles, R.drawable.chi_claire};
	
	private Face[] faces;
	
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.nuovaprova);
		
		String[] names = this.getResources().getStringArray(R.array.images_name);
		faces = new Face[facesID.length];
		
		for (int i = 0; i < 2; i++) {
			TableRow row = new TableRow(this);
			for (int j = 0; j < 4; j++) {
				FrameLayout container = new FrameLayout(this);
				container.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
				container.setBackgroundResource(R.color.image_not_selected);
				container.setForegroundGravity(Gravity.CENTER);
				
				Face imgView = new Face(names[i*4 + j], this);
				faces[i*4 + j] = imgView;
				imgView.setImageResource(facesID[i* 4 + j]);
				imgView.setPadding(10, 10, 10, 10);
				imgView.setLayoutParams(new LayoutParams(75, 100));
				imgView.setClickable(true);
				imgView.setOnClickListener(new OnClickListener() {
					
					public void onClick(View v) {
						Face face = (Face) v;
						if (face.isChoosable()) {
							face.toggle();
							/*View parent = (View) v.getParent();
							if (face.isChecked()) {
								parent.setBackgroundResource(R.color.image_selected);
							}
							else {
								parent.setBackgroundResource(R.color.image_not_selected);
							}*/
						}
					}
				});
				
				container.addView(imgView);
				row.addView(container);
			}
			TableLayout table = (TableLayout) findViewById(R.id.table_layout);
			table.addView(row, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		}
		
		Button btnEscludi = (Button)findViewById(R.id.btn_escludi);
		btnEscludi.setOnClickListener(this);
	}
	
	public void onClick(View v) {
		
		for (int i = 0; i < faces.length; i++) {
			if (faces[i].isChoosable() && faces[i].isChecked()) {
				faces[i].noMoreChoosable();
				faces[i].setImageResource(R.drawable.indovina_chi);
			}
		}
		
	}
}
